package ast.nodes.commandNodes;

import scanner.Token;
import scanner.TokenType;
import ast.ASTNode;
import ast.Evaluation;
import ast.nodes.CommandNode;
import ast.nodes.ParameterNode;

public class ReplaceNode extends CommandNode {

    public ReplaceNode()
    {
        super();
    }
    
    public ReplaceNode(Token data)
    {
        super(data);
    }
    
    /**
     * replace(a,b) modifies LINE by replacing all occurrences of a with the character b.
     */
    @Override
    public void run(Evaluation input) {
        Character a = null, b = null;
        for(ASTNode child : children)
        	if(child instanceof ParameterNode)
        		if(child.getData().getType() == TokenType.CHARACTER)
        			if(a == null)
        				a = child.getData().getTokenString().charAt(0);
        			else if(b == null)
        				b = child.getData().getTokenString().charAt(0);
        if(a != null && b != null)
	        input.replace(a.charValue(), b.charValue());
    }
}
